
/* Generated by Interface Builder */

#import "RandomView.h"
#import <appkit/Matrix.h>
#import <appkit/Application.h>
#import <appkit/PrintInfo.h>
#import <appkit/Control.h>
#import <dpsclient/wraps.h>
#import "shapes.h"

#define NUMBER_SLIDERS 8

#define CIRCLE  0
#define TRIANGLE  1
#define SQUARE  2
#define HEART   3
#define LINE  4
#define FLOWER  5
#define DIAMOND 6
#define RECTANGLE  7

#define CENTER  0
#define BOTTOM  1

@implementation RandomView

+newFrame:(const NXRect *)tF {
    self = [super newFrame:tF];
    depth = 7;
    scale = 0.8;
    rotation = 25.0;
    shrink = .75;
    numberBranches = 4;
    branchAngle = 90.0;
    over = 0.0;
    up = 100.0;
    center = CENTER;
    shapeType = HEART;
	return self;
}

// Outlets

- setTextArray:anObject
{
	textArray = anObject;
    return self;
}

- setSliderMatrix:anObject
{
	sliderMatrix = anObject;
    return self;
}

- setCenterMatrix:anObject
{
	centerMatrix = anObject;
    return self;
}

- setShapeMatrix:anObject
{
	shapeMatrix = anObject;
    return self;
}



// Action Methods

- shapeMatrix:sender
{
	shapeType = [sender selectedTag];
	[self display];
    return self;
}

- center:sender
{
	center = [sender selectedTag];
	[self display];
    return self;
}

- reset:sender
{
    depth = 7;
    [[textArray selectCellWithTag:0] setIntValue:depth];
    [[sliderMatrix selectCellWithTag:0] setIntValue:depth];
    scale = 0.8;
    [[textArray selectCellWithTag:1] setFloatValue:scale];
    [[sliderMatrix selectCellWithTag:1] setFloatValue:scale];
    rotation = 25.0;
    [[textArray selectCellWithTag:2] setFloatValue:rotation];
    [[sliderMatrix selectCellWithTag:2] setFloatValue:rotation];
    shrink = .75;
    [[textArray selectCellWithTag:3] setFloatValue:shrink];
    [[sliderMatrix selectCellWithTag:3] setFloatValue:shrink];
    numberBranches = 4;
    [[textArray selectCellWithTag:4] setIntValue:numberBranches];
    [[sliderMatrix selectCellWithTag:4] setIntValue:numberBranches];
    branchAngle = 90.0;
    [[textArray selectCellWithTag:5] setFloatValue:branchAngle];
    [[sliderMatrix selectCellWithTag:5] setFloatValue:branchAngle];
    over = 0.0;
    [[textArray selectCellWithTag:6] setFloatValue:over];
    [[sliderMatrix selectCellWithTag:6] setFloatValue:over];
    up = 100.0;
    [[textArray selectCellWithTag:7] setFloatValue:up];
    [[sliderMatrix selectCellWithTag:7] setFloatValue:up];
    center = CENTER;
    // [centerMatrix selectCellWithTag:CENTER]
    shapeType = HEART;
    // [centerMatrix selectCellWithTag:HEART]
    
	[self display];
    return self;
}


- sliderMatix:sender
{
	float senderValue;
	id textCell_ID;
	senderValue = [sender floatValue];
	sliderNumber = [sender selectedTag];
	switch (sliderNumber) {
		case 0:
			depth = [sender intValue];
			senderValue = (float) depth;
			break;
		case 1:
			scale = [sender floatValue];
			break;
		case 2:
			rotation = [sender floatValue];
			break;
		case 3:
			shrink = [sender floatValue];
			break;
		case 4:
			numberBranches = [sender floatValue];
			senderValue = (float) numberBranches;
			break;
		case 5:
			branchAngle = [sender floatValue];
			break;
		case 6:
			over = [sender floatValue];
			break;
		case 7:
			up = [sender floatValue];
			break;
		default:
			break;
		}
	textCell_ID = [textArray selectCellWithTag:sliderNumber];
	[textCell_ID setFloatValue:senderValue];
	[self display];
    return self;
}

- printPage:sender
{
	id pi = [NXApp printInfo];
	[pi setMarginLeft:0.0 right:0.0 top:0.0 bottom:0.0];
	[super printPSCode:self];
	return self;
}

- drawBranch:(int) depthNow
{
int i,j;
	if (depthNow > 1) {
    for (j=0; j<=numberBranches; j++) {
		PSgsave();
	    for (i=0; i<depth; i++) {
			switch (shapeType) {
				case CIRCLE:
					drawCircle();
					break;
				case TRIANGLE:
					drawTriangle();
					break;
				case SQUARE:
					drawSquare();
					break;
				case HEART:
					drawHeart();
					break;
				case LINE:
					drawLine();
					break;
				case FLOWER:
					drawFlower();
					break;
				default:
					printf("No shape for number %d\n", shapeType);
					break;
			} // end of switch
			PStranslate(over, up);
			PSscale(shrink, shrink);
			PSrotate(rotation);
		} // end of i for
		PSgrestore();
		PSrotate(branchAngle);
		// printf("Depth Now = %d\n", depthNow);
		// [self drawBranch: depthNow-1];
	} // end of j for
	} // if depth > 0
	return self;
}

- drawSelf:(NXRect*)r :(int)c
{
    NXEraseRect(&bounds);
    PSsetgray(NX_BLACK);

    if (center == BOTTOM)
    	PStranslate(bounds.size.width/2.0, 0.0);
	else
	    PStranslate(bounds.size.width/2.0, bounds.size.height/2.0);

	PSscale(scale, scale);
	
	[self drawBranch: depth];

    return self;
}


@end
